// travelNavigation.jquery.js
import $ from 'jquery';

// 获取元素位置信息
const getElementPositions = () => {
  // 主标题栏 -- 位置
  let nav_li_top = 0;
  if ($('.nav_li').offset() && $('.nav_li').offset().top) {
    nav_li_top = $('.nav_li').offset().top;
  }

  // 行程特色 -- 位置
  let featureTop = 0;
  if ($('#feature').offset() && $('#feature').offset().top) {
    featureTop = $('#feature').offset().top + $('#feature').height();
  }

  // 详细行程 -- 位置
  let introduction = 0;
  if ($('#introduction').offset() && $('#introduction').offset().top) {
    introduction = $('#introduction').offset().top + $('#introduction').height();
  }

  // 副标题栏 行程A B
  let subtitleTop = $('.main2_right_m2.active .main2_right_m2_title.trip_nav_m').eq(0).offset().top || 0;

  // 详细行程 main2_right_m3_m
  let stroke = $('.main2_right_m2.active').find('.main2_right_m3_m.stroke');

  return {
    nav_li_top,
    featureTop,
    introduction,
    subtitleTop,
    stroke,
  };
};

// 处理菜单导航固定逻辑
const handleNavFixing = (scrollTop, nav_li_top) => {
  if (scrollTop > nav_li_top - 80) {
    $('.nav_li').addClass('nav_li_fiexd');
    $('.main2').css('paddingTop', 40);
  } else {
    $('.nav_li').removeClass('nav_li_fiexd');
    $('.main2').css('paddingTop', 0);
  }
};

// 处理天数导航固定逻辑
const handleDayNavFixing = (scrollTop, featureTop, introduction) => {
  //console.info(scrollTop, featureTop - 50, introduction - 300);
  if (scrollTop >= featureTop - 50 && scrollTop <= introduction - 300) {
    $('.trip_m').addClass('trip_m_fiexd');
  } else {
    $('.trip_m').removeClass('trip_m_fiexd');
  }
};

// 处理菜单高亮逻辑
const handleMenuHighlighting = (scrollTop, nav_li_top) => {
  if (scrollTop >= nav_li_top - 80) {
    $('.main2')
      .find('.u_route')
      .each(function (index, e) {
        if (scrollTop >= $(e).offset().top - 160) {
          $('.nav_left .li').eq(index).addClass('active').siblings().removeClass('active');
        }
      });
  }
};

// 处理天数高度滚动逻辑
const handleDayHeightScrolling = (scrollTop, stroke) => {
  if (scrollTop <= stroke.offset().top + stroke.height() - 150) {
    $('.trip_m').show().css('opacity', 1);
    $('.main2_right_m2.active')
      .find('.stroke .main2_right_m3_m1')
      .each(function (t, e) {
        if (scrollTop >= $(e).offset().top - 150) {
          $('.u_route').find('.trip_m .li').eq(t).addClass('active').siblings().removeClass('active');
        }
      });
  } else {
    $('.trip_m').hide().css('opacity', 0);
  }
};

// 处理天数导航点击逻辑
const handleDayNavClick = () => {
  $('.trip_m .li').on('click', function () {
    let t = $(this).index();
    let targetTop = $('.main2_right_m2.active').find('.stroke .main2_right_m3_m1').eq(t).offset().top;
    $('html,body').animate({
      scrollTop: targetTop - 110,
    });
  });
};

export const setupTravelNavigation = () => {
  const { nav_li_top, featureTop, introduction, subtitleTop, stroke } = getElementPositions();

  $(window).scroll(function () {
    let scrollTop = $(window).scrollTop();

    handleNavFixing(scrollTop, nav_li_top);
    handleDayNavFixing(scrollTop, featureTop, introduction);
    handleMenuHighlighting(scrollTop, nav_li_top);
    handleDayHeightScrolling(scrollTop, stroke);
  });

  handleDayNavClick();
};
